package com.yksj.healthtalk.ui;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.universalimageloader.core.DefaultConfigurationFactory;
import org.universalimageloader.core.DisplayImageOptions;
import org.universalimageloader.core.ImageLoader;
import org.universalimageloader.core.assist.FailReason;
import org.universalimageloader.core.assist.SimpleImageLoadingListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonHttpResponseHandler;
import com.yksj.healthtalk.utils.ToastUtil;

public class TestMainActivity extends BaseFragmentActivity {
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.test_activity_layout);
		
	}
	/**
	 * latlng=39.959668,116.37943
	 * @param v
	 */
	public void getLocation(View v){
		HttpRestClient.doHttpQueryMapAddress(String.valueOf(39.959668),String.valueOf(116.37943),new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, JSONObject response) {
				super.onSuccess(statusCode, response);
				try{
					String state = response.getString("status");
					if("OK".equalsIgnoreCase(state)){
						JSONArray jsonArray2 = response.getJSONArray("results");
						response = jsonArray2.getJSONObject(0);
						String locationAddrs = response.getString("formatted_address");
					}
				}catch(JSONException e){
				}
			}
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
			}
			@Override
			public void onFinish() {
				super.onFinish();
			}
		});
	}
		
		
		
//		ListView list=	(ListView) findViewById(R.id.listview);
//		final Myadapter myadapter=new Myadapter(this);
//		list.setAdapter(myadapter);
//		
//		HttpRestClient.doHttpRequestData(null,new AsyncHttpResponseHandler(){
//			@Override
//			public void onSuccess(int statusCode, String content) {
//				super.onSuccess(statusCode, content);
//				JSONObject jsonObject;
//				try {
//					jsonObject = new JSONObject(content);
//					myadapter.OnBoundData(jsonObject.optJSONArray("yi18"));
//				} catch (JSONException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//			}
//		});
	}
	
	class Myadapter extends BaseAdapter{
		private LayoutInflater layoutInflater;
		Myadapter(Context context){
			layoutInflater=	LayoutInflater.from(context);
			loader = ImageLoader.getInstance();
		}
		JSONArray array=new JSONArray();
		private ImageLoader loader;
		
		public void OnBoundData(JSONArray array){
			this.array=array;
			notifyDataSetChanged();
			
		}
		
		@Override
		public int getCount() {
			return array.length();
		}

		@Override
		public Object getItem(int position) {
			return array.opt(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView=layoutInflater.inflate(R.layout.test_list_item, null);
			}
			TextView textView=(TextView) convertView.findViewById(R.id.textView1);
			ImageView image=(ImageView) convertView.findViewById(R.id.imageView1);
			JSONObject object=(JSONObject) getItem(position);
			textView.setText(object.optString("title"));
			textView.setTextColor(Color.BLACK);
			loader.displayImage("http://www.yi18.net/"+object.optString("img"), image);
			return convertView;
		}
		
	}

